﻿var isEnglish = false;
var arrjoblang = new Array();
var arrclass = new Array();
var arrjob = new Array();
arrjoblang[0] = '';
arrjoblang[1] = '职位选择器';
arrjoblang[2] = '关闭';
arrjoblang[3] = '请选择职位类别：';
arrjoblang[4] = '取消';
arrjoblang[5] = '对不起！最多只能选择3项！';
arrjoblang[6] = '已选项：';
arrjoblang[7] = '选择其它职位类别';


function JobSelecter(selecterName) {
    this.selecterName = selecterName;
    this.divJobData = null;
    this.objJobName = null;
    this.objWorkID = null;
    this.objJobID = null;

    this.init = function(objJobName, objWorkID, objJobID) {
        this.objJobName = objJobName;
        this.objWorkID = objWorkID;
        this.objJobID = objJobID;

        if (this.objWorkID.value != "") this.openJob(this.objWorkID.value);
        this.objJobName.value = this.getSelectedJobName();
        //this.setDefaultJob();
    }

    this.getStyle = function() {
        var str = '';
        str += '<style type="text/css">';
        str += 'a.class_open, a.class_close, a.job, a.seljob{ font-size:12px; float:left;padding:4px 1px 4px 20px;width:126px;height:14px;color:#000;text-decoration:none; }';
        str += 'a.class_open, a.class_close:hover, a.seljob:hover{ background-color:#FFCC00; }';
        str += 'a.class_open { background:url(/content/images/open.gif) no-repeat 5px center; font-weight:bold; }';
        str += 'a.class_close { background:url(/content/images/close.gif) no-repeat 5px center; }';
        str += 'a.job{ padding:1px 1px 1px 1px; width:150px;height:20px; cursor:default; }';
        str += 'a.seljob{ background:url(/content/images/del.gif) no-repeat 5px center; padding:3px 0px 0px 22px; width:88px; }';
        str += 'a.seljob:hover{ background:#FFCC00 url(/content/images/delhover.gif) no-repeat 5px center; }';
        str += '</style>';
        return str;
    }

    this.getDivBack = function() {
        var str = '<div id="divJobBack" style="filter:Alpha(Opacity=40);opacity:.4;background:#fff;z-index:999;position:absolute;left:0px;top:0px;"></div>';
        return str;
    }

    this.getIframe = function() {
        var str = '<iframe id="ifrJobMain" style="filter:Alpha(Opacity=0);opacity:0;position:absolute;width:650px;height:385px;display:none;"></iframe>';
        return str;
    }

    this.getJobData = function() {
        var str = '';
        str += '        <div id="divJobSelected" style="padding:5px;height:22px;"><span style="font-size:12px; float:left; font-weight:bold; line-height:20px">' + arrjoblang[6] + '</span></div>'; //已选项
        str += '        <div style="font-size:12px; background-color:#E9F8FE;border-top:1px dashed #006699;font-weight:bold;padding:5px; clear:both;color:#006699;">' + arrjoblang[3] + '</div>';
        str += '        <div id="divWork">' + this.getWork() + '</div>'; //大类
        str += '        <div id="divJob">' + this.getJob() + '</div>'; //小类
        this.divJobData = str;
        return str;
    }

    this.getDivJobMain = function() {
        var str = '';
        str += this.getStyle();
        str += this.getDivBack();
        str += this.getIframe();
        str += '<div id="divJobMain" style="position:absolute;width:650px;display:none;">';
        str += '<table cellpadding="0" cellspacing="0" style="width:100%;cursor:move;" id="tbJobTitle">';
        str += '    <tr>';
        str += '        <td style="background:url(/content/images/dia_t_l.gif);width:5px;height:30px;"></td>';
        str += '        <td style="background:url(/content/images/dia_t_c.gif);"><div style="float:left;color:#ffffff;font-weight:bold;font-size:14px;padding:5px;">' + arrjoblang[1] + '</div><div style="float:right;"><a title="' + arrjoblang[2] + '" id="btnClose" href="javascript:void(0);"><img onmousedown="' + this.selecterName + '.closeDiv()" src="/content/images/dia_close.gif" style="border:0px;" /></a></div></td>';
        str += '        <td style="background:url(/content/images/dia_t_r.gif);width:5px;height:30px;"></td>';
        str += '    </tr>';
        str += '</table>';
        str += '<table cellpadding="0" cellspacing="0" style="width:100%;">';
        str += '    <tr>';
        str += '        <td style="background:url(/content/images/dia_m_l.gif);width:5px;"></td>';
        str += '        <td style="height:350px; background-color:#ffffff;" valign="top">';
        str += '        <div style="padding:8px 4px 5px 1px;" id="divJobData">';
        str += this.getJobData();
        str += '        </div>';
        str += '        </td>';
        str += '        <td style="background:url(/content/images/dia_m_r.gif);width:6px;"></td>';
        str += '    </tr>';
        str += '    <tr>';
        str += '        <td style="background:url(/content/images/dia_b_l.gif);width:5px;height:6px;"></td>';
        str += '        <td style="background:url(/content/images/dia_b_c.gif);height:6px;"></td>';
        str += '        <td style="background:url(/content/images/dia_b_r.gif);width:6px;height:6px;"></td>';
        str += '    </tr>';
        str += '</table>';
        str += '</div>';
        return str;
    }

    this.getWork = function() {
        var str = '';
        str += '<table border="0" width="100%" cellpadding="0" cellspacing="5">';
        str += '    <tr>';
        str += '        <td>';
        for (var i = 0; i < arrclass.length; i++) {
            str += '        <a title="' + arrclass[i].classname + '" id="linkClass' + arrclass[i].classid + '" class="class_close" href="javascript:' + this.selecterName + '.openJob(' + arrclass[i].classid + ')">' + this.trimString(arrclass[i].classname, 9, 18) + '</a>';
        }
        str += '        </td>';
        str += '    </tr>';
        str += '</table>';
        return str;
    }

    this.getJob = function() {
        var str = '';
        for (var j = 0; j < arrclass.length; j++) {
            str += '<div id="divJob' + arrclass[j].classid + '" style="display:none">';
            str += '<table border="0" width="100%" cellpadding="0" cellspacing="5">';
            str += '    <tr>';
            str += '        <td><a class="class_open" style="float:left;width:auto;" href="javascript:' + this.selecterName + '.openJob(' + arrclass[j].classid + ')">' + arrclass[j].classname + '</a><a style="font-size:12px; float:left; margin-left:10px; padding-top:2px;text-decoration:none;color:#000;" href="javascript:' + this.selecterName + '.openJob(' + arrclass[j].classid + ')">[ ' + arrjoblang[7] + ' ]</a></td>';
            str += '    </tr>';
            str += '    <tr>';
            str += '        <td id="tdJob' + arrclass[j].classid + '"></td>';
            str += '    </tr>';
            str += '</table>';
            str += '</div>';
        }
        return str;
    }

    this.trimString = function(str, cnLen, enLen) {
        var maxLen = isEnglish ? enLen : cnLen;
        if (str.length > maxLen)
            str = str.substr(0, maxLen) + "...";
        return str;
    }

    this.getJobName = function(jobid) {
        var i;
        for (i = 0; i < arrjob.length; i++) {
            if (arrjob[i].itemid == jobid)
                return arrjob[i].itemname;
        }
        return "";
    }

    this.getSelectedJobName = function() {
        var idstr = this.objJobID.value;
        if (idstr == "") return "";
        if (idstr.indexOf(",") == -1) return this.getJobName(idstr);
        var arrid = idstr.split(",");
        var rvalue = "";
        for (var i = 0; i < arrid.length; i++) {
            var jobName = this.getJobName(arrid[i]);
            if (rvalue == "") rvalue = jobName;
            else if (jobName != "") { rvalue += "+" + jobName; }
        }
        return rvalue;
    }

    this.selectJob = function(sender, jobName) {
        var chJob = document.getElementById("chJob" + sender.value);

        if (sender.checked) {
            var val = this.objJobID.value.split(",");
            if (val.length > 2) {
                alert(arrjoblang[5]);
                return false;
            }
            if (this.objJobID.value == "") this.objJobID.value = sender.value;
            else this.objJobID.value += ',' + sender.value;
            this.objJobName.value = this.getSelectedJobName();
            this.addJob(sender.value, jobName);

            if (chJob && sender != chJob) chJob.checked = true;

            // 如果选择大类不限
            if (parseInt(sender.value) >= 10000) this.delOtherJob(parseInt(sender.value) / 10000);
        }
        else {
            if (chJob && sender != chJob) chJob.checked = false;
            this.delJob(sender.value);

            // 如果选择大类不限
            if (parseInt(sender.value) >= 10000) this.setWorkDisabled(parseInt(sender.value) / 10000, "");
        }
    }

    this.addJob = function(jobID, jobName) {
        var linkSelJob = getElem("linkSelJob" + jobID, "a", document.getElementById("divJobSelected"));
        linkSelJob.className = "seljob";
        linkSelJob.innerHTML = this.trimString(jobName, 5, 10);
        linkSelJob.href = "javascript:" + this.selecterName + ".delSelectedJob(" + jobID + ")";
        linkSelJob.title = arrjoblang[4] + jobName;
    }

    this.delSelectedJob = function(jobID) {
        this.delJob(jobID);

        var chJob = document.getElementById("chJob" + jobID);
        if (chJob) chJob.checked = false;

        // 如果选择大类不限
        if (jobID >= 10000) this.setWorkDisabled(jobID / 10000, "");
    }

    this.delJob = function(jobID) {
        var val = this.objJobID.value.split(",");
        var jobvalue = "";
        for (var i = 0; i < val.length; i++) {
            if (val[i] != jobID) {
                if (jobvalue == "") jobvalue = val[i];
                else jobvalue += ',' + val[i];
            }
        }
        this.objJobID.value = jobvalue;
        this.objJobName.value = this.getSelectedJobName();

        var linkSelJob = document.getElementById("linkSelJob" + jobID);
        var divJobSelected = document.getElementById("divJobSelected");
        if (linkSelJob) divJobSelected.removeChild(linkSelJob);
    }

    this.delOtherJob = function(classID) {
        for (var i = 0; i < arrjob.length; i++) {
            if (arrjob[i].classid == classID && arrjob[i].itemid < 10000) {
                this.delSelectedJob(arrjob[i].itemid);
                this.setJobDisabled(arrjob[i].itemid, "disabled");
            }
        }
    }

    this.setWorkDisabled = function(classID, disabled) {
        for (var i = 0; i < arrjob.length; i++) {
            if (arrjob[i].classid == classID && arrjob[i].itemid < 10000) {
                var chJob = document.getElementById("chJob" + arrjob[i].itemid);
                if (chJob) chJob.disabled = disabled;
            }
        }
    }

    this.setJobDisabled = function(jobID, disabled) {
        var chJob = document.getElementById("chJob" + jobID);
        if (chJob) chJob.disabled = disabled;
    }

    this.getClassID = function(jobid) {
        var i;
        for (i = 0; i < arrjob.length; i++) {
            if (arrjob[i].itemid == jobid)
                return arrjob[i].classid;
        }
        return 0;
    }

    this.closeDiv = function() {
        var divJobBack = document.getElementById("divJobBack");
        divJobBack.style.display = "none";

        var divJobMain = document.getElementById("divJobMain");
        divJobMain.style.display = "none";

        var ifrJobMain = document.getElementById("ifrJobMain");
        ifrJobMain.style.display = "none";
    }

    this.getTdJobData = function(workId) {
        var str = '';
        for (var i = 0; i < arrjob.length; i++) {
            if (arrjob[i].classid == workId) {
                str += '        <a title="' + arrjob[i].itemname + '" id="linkJob' + arrjob[i].itemid + '" class="job" href="javascript:void(0)"' + (arrjob[i].itemid >= 10000 ? 'style="width:100%;"' : '') + '><input type="checkbox" id="chJob' + arrjob[i].itemid + '" value="' + arrjob[i].itemid + '" onclick="return ' + this.selecterName + '.selectJob(this, \'' + arrjob[i].itemname + '\')" /><label for="chJob' + arrjob[i].itemid + '">' + (arrjob[i].itemid >= 10000 ? arrjob[i].itemname : this.trimString(arrjob[i].itemname, 9, 16)) + '</label></a>';
            }
        }
        return str;
    }

    this.openJob = function(workId) {
        var divWork = document.getElementById("divWork");
        var divJob1 = document.getElementById("divJob" + workId);
        var tdJob1 = document.getElementById("tdJob" + workId);
        var linkClass1 = document.getElementById("linkClass" + workId);

        if (divJob1.style.display == "") {
            divWork.style.display = "";
            divJob1.style.display = "none";
            linkClass1.className = "class_close";
        }
        else {
            //            for (var i = 0; i < arrclass.length; i++) {
            //                var divJob2 = document.getElementById("divJob" + arrclass[i].classid);
            //                var linkClass2 = document.getElementById("linkClass" + arrclass[i].classid);

            //                divJob2.style.display = "none";
            //                linkClass2.className = "class_close";
            //            }
            if (tdJob1.innerHTML == "") {
                tdJob1.innerHTML = this.getTdJobData(workId);
                this.setDefaultJob();
            }
            divWork.style.display = "none";
            divJob1.style.display = "";
            linkClass1.className = "class_open";
        }
    }

    this.setDefaultJob = function() {
        var idstr = this.objJobID.value;
        if (idstr == "") return;

        var arrid = idstr.split(",");
        for (var i = 0; i < arrid.length; i++) {
            // 在选择器中显示默认选中小类
            this.addJob(arrid[i], this.getJobName(arrid[i]));

            // 如果选择大类不限
            if (arrid[i] >= 10000) this.setWorkDisabled(arrid[i] / 10000, "disabled");

            // 把默认选中的小类打上勾
            var chJob = document.getElementById("chJob" + arrid[i]);
            if (chJob) chJob.checked = true;
        }
    }

    this.show = function(objJobName, objWorkID, objJobID, showWork) {
        var divJobBack = document.getElementById("divJobBack");
        divJobBack.style.width = document.documentElement.clientWidth + "px";
        divJobBack.style.height = document.documentElement.scrollHeight + "px";
        if (getInt(divJobBack.style.height) < 768) divJobBack.style.height = "768px";
        divJobBack.style.display = "";

        var divJobMain = document.getElementById("divJobMain");
        divJobMain.style.left = ((document.documentElement.clientWidth - getInt(divJobMain.style.width)) / 2) + "px";
        divJobMain.style.top = ((screen.availHeight - 400) / 2 + document.documentElement.scrollTop - 100) + "px";
        divJobMain.style.zIndex = 10000;
        divJobMain.style.display = "";

        var ifrJobMain = document.getElementById("ifrJobMain");
        ifrJobMain.style.left = divJobMain.style.left;
        ifrJobMain.style.top = divJobMain.style.top;
        ifrJobMain.style.zIndex = divJobMain.style.zIndex - 1;
        ifrJobMain.style.display = "";

        var tbJobTitle = document.getElementById("tbJobTitle");
        tbJobTitle.onmousedown = function(evt) {
            curMoveObj = divJobMain;
            curMoveIframe = ifrJobMain;
            if (evt) { //firefox
                pX = evt.pageX - getInt(curMoveObj.style.left);
                pY = evt.pageY - getInt(curMoveObj.style.top);
            }
            else { //ie
                pX = event.x - curMoveObj.style.pixelLeft;
                pY = event.y - curMoveObj.style.pixelTop;
            }
        }

        // 复原数据区
        var divJobData = document.getElementById("divJobData");
        divJobData.innerHTML = this.divJobData;

        // 隐藏不限选项
        if (!showWork) {
            for (var i = 0; i < arrclass.length; i++) {
                var linkJob = document.getElementById("linkJob" + (arrclass[i].classid * 10000));
                if (linkJob) linkJob.style.display = "none";
            }
        }

        // 初始化控件
        this.init(objJobName, objWorkID, objJobID);

        // 显示默认选中小类
        this.setDefaultJob();
    }

    this.toString = function() {
        return this.getDivJobMain();
    }
}
var jobSelecter;
$(function() {
    $.getScript("/ajax/GetJobList.aspx", function(data) {
        eval(data);
        jobSelecter = new JobSelecter("jobSelecter");
        $("body").append(jobSelecter.toString());
    });
});